@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion

REM 文档转换器 - 一键启动脚本 (Windows)
REM 同时启动后端Flask服务器和前端Vite开发服务器

title 文档转换器 - 一键启动

REM 颜色定义 (Windows 10+ 支持ANSI颜色)
set "RED=[31m"
set "GREEN=[32m"
set "YELLOW=[33m"
set "BLUE=[34m"
set "PURPLE=[35m"
set "CYAN=[36m"
set "NC=[0m"

REM 打印带颜色的消息
:print_message
echo %~2%~1%NC%
goto :eof

REM 打印标题
:print_title
echo.
echo %CYAN%================================%NC%
echo %CYAN%  %~1%NC%
echo %CYAN%================================%NC%
echo.
goto :eof

REM 检查命令是否存在
:check_command
where %1 >nul 2>&1
if errorlevel 1 (
    call :print_message "❌ 错误: %1 未安装或不在PATH中" "%RED%"
    pause
    exit /b 1
)
goto :eof

REM 检查目录是否存在
:check_directory
if not exist "%~1" (
    call :print_message "❌ 错误: 目录 %~1 不存在" "%RED%"
    pause
    exit /b 1
)
goto :eof

REM 检查文件是否存在
:check_file
if not exist "%~1" (
    call :print_message "❌ 错误: 文件 %~1 不存在" "%RED%"
    pause
    exit /b 1
)
goto :eof

REM 从配置文件读取服务器配置
:read_config
:: 从config.yaml读取配置信息
for /f "delims=" %%i in ('python -c "import yaml; config = yaml.safe_load(open('config.yaml', 'r', encoding='utf-8')); backend_host = config.get('server', {}).get('host'); backend_port = config.get('server', {}).get('port'); frontend_host = config.get('frontend', {}).get('host'); frontend_port = config.get('frontend', {}).get('port'); print(f'{backend_host}:{backend_port}:{frontend_host}:{frontend_port}')" 2^>nul') do (
    set "config_output=%%i"
)

if not defined config_output (
    call :print_message "❌ 无法读取配置文件" "%RED%"
    exit /b 1
)

for /f "tokens=1,2,3,4 delims=:" %%a in ("%config_output%") do (
    set "SERVER_HOST=%%a"
    set "SERVER_PORT=%%b"
    set "FRONTEND_HOST=%%c"
    set "FRONTEND_PORT=%%d"
)
goto :eof

REM 清理函数
:cleanup
call :print_message "🛑 正在停止服务..." "%YELLOW%"
if defined BACKEND_PID (
    taskkill /PID %BACKEND_PID% /F >nul 2>&1
    call :print_message "✅ 后端服务已停止" "%GREEN%"
)
if defined FRONTEND_PID (
    taskkill /PID %FRONTEND_PID% /F >nul 2>&1
    call :print_message "✅ 前端服务已停止" "%GREEN%"
)
call :print_message "👋 服务已全部停止" "%BLUE%"
goto :eof

REM 主程序开始
call :print_title "🚀 文档转换器 - 一键启动脚本"

REM 检查必要的命令
call :print_message "🔍 检查系统环境..." "%BLUE%"
call :check_command "python"
call :check_command "node"
call :check_command "npm"

REM 检查项目结构
call :print_message "📁 检查项目结构..." "%BLUE%"
call :check_file "run.py"
call :check_file "requirements.txt"
call :check_file "config.yaml"
call :check_directory "backend"
call :check_directory "frontend"
call :check_file "frontend\package.json"

call :print_message "✅ 环境检查通过" "%GREEN%"

REM 读取配置文件
call :print_message "📖 读取配置文件..." "%BLUE%"
call :read_config
call :print_message "✅ 配置读取成功: %SERVER_HOST%:%SERVER_PORT% 和 %FRONTEND_HOST%:%FRONTEND_PORT%" "%GREEN%"

REM 安装Python依赖
call :print_title "📦 安装Python依赖"
if not exist ".venv\Scripts\activate.bat" (
    call :print_message "🔧 创建Python虚拟环境..." "%YELLOW%"
    python -m venv .venv
)

call :print_message "🔧 激活虚拟环境..." "%YELLOW%"
call .venv\Scripts\activate.bat

call :print_message "📥 安装Python依赖..." "%YELLOW%"
pip install -r requirements.txt

REM 安装Node.js依赖
call :print_title "📦 安装Node.js依赖"
cd frontend
if not exist "node_modules" (
    call :print_message "📥 安装Node.js依赖..." "%YELLOW%"
    npm install
) else (
    call :print_message "✅ Node.js依赖已存在" "%GREEN%"
)
cd ..

REM 启动后端服务
call :print_title "🖥️  启动后端服务"
call :print_message "🚀 启动Flask服务器..." "%YELLOW%"

REM 在新窗口中启动后端
start "后端服务器" /min cmd /c "call .venv\Scripts\activate.bat && python run.py --host %SERVER_HOST% --port %SERVER_PORT%"

REM 等待后端启动
timeout /t 5 /nobreak >nul

call :print_message "✅ 后端服务已启动" "%GREEN%"
call :print_message "🌐 后端地址: http://%SERVER_HOST%:%SERVER_PORT%" "%CYAN%"

REM 启动前端服务
call :print_title "🎨 启动前端服务"
call :print_message "🚀 启动Vite开发服务器..." "%YELLOW%"

REM 在新窗口中启动前端
start "前端服务器" /min cmd /c "cd frontend && set VITE_PORT=%FRONTEND_PORT% && npm start"

call :print_message "✅ 前端服务已启动" "%GREEN%"
call :print_message "🌐 前端地址: http://%FRONTEND_HOST%:%FRONTEND_PORT%" "%CYAN%"

REM 显示服务信息
call :print_title "🎉 服务启动完成"
call :print_message "📍 后端API: http://%SERVER_HOST%:%SERVER_PORT%" "%PURPLE%"
call :print_message "📍 前端界面: http://%FRONTEND_HOST%:%FRONTEND_PORT%" "%PURPLE%"
call :print_message "📍 健康检查: http://%SERVER_HOST%:%SERVER_PORT%/api/health" "%PURPLE%"
echo.
call :print_message "💡 提示:" "%BLUE%"
call :print_message "   - 关闭此窗口或按任意键停止所有服务" "%BLUE%"
call :print_message "   - 前端会自动代理API请求到后端" "%BLUE%"
call :print_message "   - 修改代码后前端会自动热重载" "%BLUE%"
call :print_message "   - 后端和前端在独立窗口中运行" "%BLUE%"
call :print_message "   - 服务器配置来自 config.yaml 文件" "%BLUE%"

echo.
call :print_message "⏳ 服务运行中... 按任意键停止所有服务" "%YELLOW%"

REM 等待用户按键
pause >nul

REM 停止服务
call :print_message "🛑 正在停止所有服务..." "%YELLOW%"

REM 关闭后端和前端窗口
taskkill /FI "WindowTitle eq 后端服务器*" /F >nul 2>&1
taskkill /FI "WindowTitle eq 前端服务器*" /F >nul 2>&1

REM 强制关闭可能残留的进程
for /f "tokens=2" %%i in ('tasklist /FI "IMAGENAME eq python.exe" /FO CSV ^| findstr "run.py"') do (
    taskkill /PID %%i /F >nul 2>&1
)

for /f "tokens=2" %%i in ('tasklist /FI "IMAGENAME eq node.exe" /FO CSV ^| findstr "vite"') do (
    taskkill /PID %%i /F >nul 2>&1
)

call :print_message "✅ 所有服务已停止" "%GREEN%"
call :print_message "👋 感谢使用文档转换器！" "%BLUE%"

pause 